package com.ruoyi.project.data.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.service.PermissionService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.data.domain.WeightRecord;
import com.ruoyi.project.data.domain.WeightStation;
import com.ruoyi.project.data.service.AbnormalService;
import com.ruoyi.project.system.domain.SysConfig;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  异常
 */
@RestController
@RequestMapping("/abnormal")
public class AbnormalController  extends BaseController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private AbnormalService abnormalService;

    /**
     * 获取称重异常
     * @return
     */
    @GetMapping(value = "/getCzycData")
    public TableDataInfo getCzycData(@RequestParam Map<String,Object> param){
          if (CollectionUtils.isEmpty(param)){
              param=new HashMap<>();
          }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        param.put("stationId", stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        param.put("areas",areas);
        param.put("managementCharacters",managementCharacters);
        startPage();
        List<Map<String,Object>> resList=abnormalService.selectCzYcDataList(param);

        return  getDataTable(resList);
    }

    /**
     * 车牌异常数据
     * @param param
     * @return
     */
    @RequestMapping(value = "/getCpycData")
    public TableDataInfo  getCpycData(@RequestParam Map<String,Object> param){
        if (CollectionUtils.isEmpty(param)){
            param=new HashMap<>();
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        param.put("stationId",stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        param.put("areas",areas);
        param.put("managementCharacters",managementCharacters);
        startPage();
        List<Map<String,Object>> resList=abnormalService.selectCpYcDataList(param);

        return  getDataTable(resList);
    }

    /**
     * 运单异常
     * @param param
     * @return
     */
    @RequestMapping(value = "/getYdycData")
    public TableDataInfo getYdycData(@RequestParam Map<String,Object> param){
        if (CollectionUtils.isEmpty(param)){
            param=new HashMap<>();
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        param.put("stationId", stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        param.put("areas",areas);
        param.put("managementCharacters",managementCharacters);
        startPage();
        List<Map<String,Object>> resList=abnormalService.selectYdYcDataList(param);

        return  getDataTable(resList);
    }


    /**
     * 无称重记录出场
     * @param param
     * @return
     */
    @RequestMapping(value = "/getWczjlccData")
    public TableDataInfo getWczjlccData(@RequestParam Map<String,Object> param){
        if (CollectionUtils.isEmpty(param)){
            param=new HashMap<>();
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        param.put("stationId", stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        param.put("areas",areas);
        param.put("managementCharacters",managementCharacters);
        startPage();
        PageHelper.startPage(Integer.parseInt(param.get("pageNum").toString()),Integer.parseInt(param.get("pageSize").toString()));
        List<Map<String,Object>> resList=abnormalService.selectWczjlccDataList(param);

        return  getDataTable(resList);
    }

    /**
     * 24小时无称重记录
     * @param param
     * @return
     */
    @RequestMapping(value = "/getWczjlData")
    public TableDataInfo getWczjlData(@RequestParam Map<String,Object> param){
        if (CollectionUtils.isEmpty(param)){
            param=new HashMap<>();
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        param.put("stationId", stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        param.put("areas",areas);
        param.put("managementCharacters",managementCharacters);
        SysConfig config = abnormalService.getPercentage();
        if (config != null){
            param.put("percentage",Double.parseDouble(config.getConfigValue()));
        }else {
            param.put("percentage",0.5);
        }
        startPage();
        List<Map<String,Object>> resList=abnormalService.selectWczjlDataList(param);

        return  getDataTable(resList);
    }


    /**
     * 超载出场
     * @param param
     * @return
     */
    @RequestMapping(value = "/getCzccData")
    public TableDataInfo getCzccData(@RequestParam Map<String,Object> param){
        if (CollectionUtils.isEmpty(param)){
            param=new HashMap<>();
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        param.put("stationId", stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        param.put("areas",areas);
        param.put("managementCharacters",managementCharacters);
        startPage();
        List<Map<String,Object>> resList=abnormalService.selectCzccDataList(param);

        return  getDataTable(resList);
    }

    /**
     * 无称重记录异常
     * @param param
     * @return
     */
    @RequestMapping(value = "/noRecordabnormal")
    public  TableDataInfo noRecordabnormal(@RequestParam Map<String,Object> param){
        if (CollectionUtils.isEmpty(param)){
            param=new HashMap<>();
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        param.put("stationId",stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        param.put("areas",areas);
        param.put("managementCharacters",managementCharacters);
        startPage();
        List<Map<String,Object>> resList=abnormalService.selectNoRecordabnormal(param);

        return  getDataTable(resList);
    }

    @GetMapping("/nowaybillrecord")
    public TableDataInfo nowaybillrecord(WeightRecord weightRecord)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        weightRecord.setStationId(stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        weightRecord.setAreas(areas);
        weightRecord.setManagementCharacters(managementCharacters);
        startPage();
        List<Map<String, Object>> list = abnormalService.selectNoWaybillWeightRecordList(weightRecord);
        if(list != null && list.size() > 0) {
            for(Map<String, Object> record : list) {
                Map<String, Object> carMap = new HashMap<String, Object>();
                if (record.get("plantNoGua") != null) {
                    if (!record.get("plantNoGua").equals(record.get("plantNo"))) {
                        if ("-".equals(record.get("plantNoGua")) || "无牌车".equals(record.get("plantNoGua"))) {
                            record.put("plantNoGua", "无");
                        }
                    } else {
                        record.put("plantNoGua", "无");
                    }
                } else {
                    record.put("plantNoGua", "无");
                }
            }
        }
        return getDataTable(list);
    }
}
